const db = require('./db')
const express = require('express')
const port = 3000

const app = express()

app.set('x-powered-by',false)
app.use(express.urlencoded({extended:true}))
app.use(express.json())

//引入session，做cookie持久化
const session = require('express-session')
const MongoStore = require('connect-mongo')

app.use(session({
    name:'_id',  //返回给客户端cookie的key
    secret:'cangjie TNT',  //参与加密的字符串
    saveUninitialized:false,  //是否在存储内容之前创建session对话
    resave:true,  //是否在每次请求时，强制重新保存session，即使它们没有变化
    store:MongoStore.create({
    	mongoUrl:'mongodb://localhost:27017/sessions_container',
    	touchAfter:60  //修改频率（例：24h只更新一次）1:1s
	}),
    cookie:{
        httpOnly:true,  // 开启后前端无法通过JS操作cookie
    	maxAge:1000*60*60*24*365,  //设置cookie的过期时间 1000:1s
	}
}))

const roleRouter = require('./router/roleRouter')
const userRouter = require('./router/userRouter')

db.then(()=>{
    console.log('数据库连接成功！！！');
    app.use(roleRouter())
    app.use(userRouter())
},(err)=>{
    console.log('数据库连接失败');
    console.log(err);
}).catch((error)=>{
    console.log(error);
})

app.listen(port, () => {
    console.log(`服务器启动成功，正在监听 ${port} 端口!`)
})

